package com.star.pickstarbackend.service.impl;

import com.star.pickstarbackend.mapper.UserMapper;
import com.star.pickstarbackend.pojo.User;
import com.star.pickstarbackend.service.UserService;
import com.star.pickstarbackend.utils.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.star.pickstarbackend.utils.ThreadLocalUtil;

import java.util.Map;

@Service
public class UserServiceImpl  implements UserService {
    @Autowired
    private UserMapper userMapper;


    @Override
    public User findByUsername(String username) {
        User u = userMapper.findByUsername(username);
        return u;
    }

    @Override
    public void register(String username, String password , String email, String phone, String realname , String role, String avatar) {
        User user = new User();
        user.setUsername(username);
        user.setPassword(Md5Util.getMD5String(password));
        user.setRealName(realname);
        user.setEmail(email);
        user.setPhone(phone);
        user.setRole(role);
        user.setStatus(1);
        user.setAvatar(avatar);
        userMapper.insertUser(user);
    }

    @Override
    public void update(User user) {
        userMapper.update(user);
    }

    @Override
    public void update(String avatarUrl, Integer id) {
        userMapper.updateAvatar(avatarUrl,id);
    }

    @Override
    public void updatePwd(String newPwd) {
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        userMapper.updatePwd(Md5Util.getMD5String(newPwd),id);
    }
}
